package Day020_NIO_AIO.demo2_NIO2_AIO.Test2_AIO异步连接;

import java.net.InetSocketAddress;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;

/**
 * @author zhangyan
 * @date 2020/5/13 14:06
 */
//没有意义

public class Server {
    public static void main(String[] args) throws Exception{
        /*
            异步:无返回值,有回调函数
            void accept();
            参数1:附件 没用 传入null
            参数2:CompletionHandler<V,A>，AIO中处理事件的接口,该类中有2个抽象方法(回调函数)
                 连接成功 void completed(V result, A attachment);
                 连接失败 void failed(Throwable exc, A attachment);
        */

        //1. 获取服务器通道对象
        AsynchronousServerSocketChannel assc = AsynchronousServerSocketChannel.open();

        //2. 对象调用bind()方法 绑定团口号
        assc.bind(new InetSocketAddress(666));

        System.out.println(1);
        //3. accept()异步的方法,等待接收请求
        //匿名内部类
        assc.accept(null, new CompletionHandler<AsynchronousSocketChannel, Object>() {
            @Override
            public void completed(AsynchronousSocketChannel result, Object attachment) {
                //如果客户端连接 执行completed()
                System.out.println(3);
            }

            @Override
            public void failed(Throwable exc, Object attachment) {
                //如果客户端连接失败 执行failed()
            }
        });
        System.out.println(2);

        while (true){}

        //打印1 2,客户端连接后打印3
    }
}
